<?php
class model_intall extends database_mysql
{
    public $layout = NULL;
    public function __construct()
    {
        $this->view = new system_view();
        $this->model = new model__model_model();
        $this->rt = new handle_route();   
        $this->_seokey = $this->rt->rt_get_all_val_seo();
    }
    
    public function index_controller()
    {
        $html = '';
        $_seokey = $this->rt->rt_get_all_val_seo();
        $action = isset($_seokey['2'])?$_seokey['2']:'';
        
        switch($action){
            case '':
                $html = $this->add_controller();
            break;
            case 'ajax':
                $html = $this->ajax_controller();
            break;
        }
        return $html;
    }
    
    public function add_controller(){
        $error = '';
        $success = '';
        $app = $this->_seokey[0];
        $strHtml = $this->view->view_display('add');
        $arrHtml = $this->view->view_get_block_html($strHtml);
        $list_com = $this->model->md_cm_get_list_com($app);
        if(!empty($list_com)){
            $strOkHtml = '';
            foreach($list_com as $value){
                $strOkHtml .= $this->view->view_assign($arrHtml['com'],array(
                                                                            'com_value'                 => $value['Com'],
                                                                            'com_id'                    => $value['Com']
                                                                        ));
            }
        }else{
            $strHtmlContent = $this->view->view_assign_block($strHtml, array(
                                                                    'list_com'=>''));
            $success = 'Hiện tại không có Com trong database';
            $strHtmlContent = $this->view->view_assign($strHtmlContent,array('error'=>$error, 
                                                                         'app'=>$app,
                                                                         'success'=>$success ));
            return $strHtmlContent;
        }

        if(isset($_POST) && !empty($_POST['model']) && !empty($_POST['com'])){
            $com = $_POST['com'];
            $app = $_POST['app'];
            $model = trim($_POST['model']);
            $model_path = realpath('../') . '/app/' . $app . '/model/' . $model . '.php';
            if(!file_exists($model_path)){
                $model_path_tmp = realpath('./temp/model/demo.php');
                $content_source = file_get_contents($model_path_tmp);
                $content_source = str_replace('demo',$model,$content_source);
                $fh = fopen($model_path, 'w+');
                fwrite($fh, $content_source);
                fclose($fh);
                
            }
            $success = 'Model đã thêm thành công!';
        }
        $strHtmlContent = $this->view->view_assign_block($strHtml, array(
                                                                    'list_com'=>$strOkHtml));
        $strHtmlContent = $this->view->view_assign($strHtmlContent,array('error'=>$error, 
                                                                         'app'=>$app,
                                                                         'success'=>$success ));
        
        
        return $strHtmlContent;

            
    }
}

